package com.bytedance.frameworks.baselib.a;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

class a {
    final class a$a extends SQLiteOpenHelper {
        public a$a(Context arg4) {
            super(arg4, "lib_log_queue.db", null, 1);
        }

        public final void onCreate(SQLiteDatabase arg4) {
            try {
                arg4.execSQL("CREATE TABLE queue ( _id INTEGER PRIMARY KEY AUTOINCREMENT, value BLOB, type TEXT, timestamp INTEGER, retry_count INTEGER, retry_time INTEGER )");
            } catch (Exception v0) {
                new StringBuilder("create db exception ").append(v0);
            }
        }

        public final void onUpgrade(SQLiteDatabase arg1, int arg2, int arg3) {
        }
    }

    private static String[] a = new String[]{"_id", "value", "type", "timestamp", "retry_count", "retry_time"};
    private static a b;
    private SQLiteDatabase c;

    private a(Context arg3) {
        if (arg3 != null) {
            try {
                this.c = new a$a(arg3.getApplicationContext()).getWritableDatabase();
            } catch (Throwable v0) {
            }
        }
    }

    static a a(Context arg2) {
        if (b == null) {
            Class v1 = a.class;
            synchronized (v1) {
                if (b == null) {
                    b = new a(arg2);
                }
            }
        }

        return b;
    }

    private static void a(Cursor arg1) {
        if (arg1 != null) {
            try {
                if (arg1.isClosed()) {
                    return;
                }

                arg1.close();
            } catch (Exception v0) {
            }
        }
    }

    final long a() {
        long v0 = 0;
        Cursor v2 = null;
        synchronized (this) {
            try {
                if (this.c()) {
                    String v3_1 = "select count(*) from queue";
                    if (!TextUtils.isEmpty(null)) {
                        v3_1 = v3_1 + " " + null;
                    }
                    v2 = this.c.rawQuery(v3_1, null);
                    if (v2.moveToNext()) {
                        v0 = v2.getLong(0);
                    }
                    return v0;
                }
                return v0;
            } catch (Throwable v0_1) {
                return v0;
            } finally {
                a(v2);
            }
        }
    }

    final long a(String arg5, byte[] arg6) {
        synchronized (this) {
            if (!this.c()) {
                return -1;
            } else if (arg6 == null) {
                return -1;
            } else if (arg6.length > 0) {
                ContentValues v0_1 = new ContentValues();
                v0_1.put("value", arg6);
                v0_1.put("type", arg5);
                v0_1.put("timestamp", Long.valueOf(System.currentTimeMillis()));
                v0_1.put("retry_count", Integer.valueOf(0));
                v0_1.put("retry_time", Long.valueOf(0));
                return this.c.insert("queue", null, v0_1);
            } else {
                return -1;
            }
        }
    }

    final c a(long arg12) {
        c v1_1;
        Exception v1;
        Cursor v2 = null;
        c v9 = null;
        c v0_1 = null;
        synchronized (this) {
            try {
                if (this.c()) {
                } else {
                    return null;
                }
                v2 = this.c.query("queue", a, "_id > ?", new String[]{String.valueOf(arg12)}, null, null, "_id ASC", "1");
                if (v2.moveToNext()) {
                    v1_1 = new c();
                    v1_1.a = v2.getLong(0);
                    v1_1.b = v2.getBlob(1);
                    v1_1.e = v2.getString(2);
                    v2.getLong(3);
                    v1_1.c = v2.getInt(4);
                    v1_1.d = v2.getLong(5);
                    v0_1 = v1_1;
                } else {
                    v0_1 = v9;
                }
                return v0_1;

            } catch (Exception v0_2) {
                new StringBuilder("getLog exception ").append(v0_2.getMessage());
            } finally {
                a(v2);
            }
            return v0_1;
        }
    }

    final void a(String arg6, int arg7, long arg8) {
        String[] v1;
        String v0_1;
        synchronized (this) {
            if (!this.c()) {
                return;
            }

            long v2 = System.currentTimeMillis() - arg8;
            if (TextUtils.isEmpty(((CharSequence) arg6))) {
                v0_1 = "timestamp <= ? ";
                v1 = new String[]{String.valueOf(v2)};
            } else {
                v0_1 = "(timestamp <= ? OR retry_count > " + arg7 + ") and type = ?";
                v1 = new String[]{String.valueOf(v2), arg6};
            }
            this.c.delete("queue", v0_1, v1);
        }
    }

    final boolean a(long arg14, boolean arg16, long arg17, int arg19) {
        boolean v2_3;
        Cursor v3;
        String[] v6;
        synchronized (this) {
            if (this.c()) {
            } else {
                return false;
            }

            if (arg14 > 0) {
                try {
                    v6 = new String[]{String.valueOf(arg14)};
                    if (!arg16) {

                    } else {
                        this.c.delete("queue", "_id = ?", v6);
                        return false;
                    }

                    Cursor v10 = null;
                    int v2_1 = 2;
                    String[] v4 = new String[v2_1];
                    v4[0] = "timestamp";
                    v4[1] = "retry_count";
                    v3 = this.c.query("queue", v4, "_id = ?", v6, null, null, null);
                    if (!v3.moveToNext()) {
                        return false;
                    } else {
                        try {
                            long v4_1 = v3.getLong(0);
                            v2_1 = v3.getInt(1);
                            long v8 = System.currentTimeMillis();
                            if (v8 - v4_1 < arg17 && v2_1 < arg19) {
                                ContentValues v4_2 = new ContentValues();
                                v4_2.put("retry_count", Integer.valueOf(v2_1 + 1));
                                v4_2.put("retry_time", Long.valueOf(v8));
                                this.c.update("queue", v4_2, "_id = ?", v6);
                                return true;
                            }
                            this.c.delete("queue", "_id = ?", v6);
                            return false;
                        } catch (Exception v2_2) {
                            new StringBuilder("onLogSent exception: ").append(v2_2);
                        } finally {
                            a(v3);
                            return false;
                        }
                    }
                } catch (Exception v2_2) {
                    v2_2.printStackTrace();
                }
            }
            return false;
        }
    }

    final void b() {
        synchronized (this) {
            if (this.c()) {
                try {
                    this.c.execSQL("DROP TABLE IF EXISTS queue");
                    this.c.execSQL("CREATE TABLE queue ( _id INTEGER PRIMARY KEY AUTOINCREMENT, value BLOB, type TEXT, timestamp INTEGER, retry_count INTEGER, retry_time INTEGER )");
                } catch (Exception v0_1) {
                    new StringBuilder("recreateTableQueue db exception ").append(v0_1);
                }
            }

            return;
        }
    }

    private boolean c() {
        synchronized (this) {
            if (this.c == null) {
            } else if (this.c.isOpen()) {
                return true;
            }

            return false;
        }
    }
}

